package com.haohope.framework;

import java.util.Properties;

import com.alibaba.druid.util.StringUtils;
import com.haohope.framework.datasource.DBType;
import com.wb.util.StringUtil;

public class FrameworkConstant {
	private FrameworkConstant() {}
	public final static class SystemParam {
		public final static String DEBUG = "framework.debug";
		public final static String WEBPORT = "framework.webport";
		public final static String WEBPATH = "framework.webpath";
		public final static String DBTYPE = "framework.dbtype";
		public final static String DBIP = "framework.dbip";
		public final static String DBPORT = "framework.dbport";
		public final static String DBSCHEMA = "framework.dbschema";
		public final static String DEFAULTDBIP = "dbhost";
		public final static Integer DEFAULTDBPORT = 3306;
		public final static String DEFAULTDBSCHEMANAME = "db_wb";
		public final static String DBUSERNAME = "framework.dbusername";
		public final static String DBPASSWORD = "framework.dbpasswd";
		public final static String ACCESSLOGPOOLSIZE = "framework.accesslogPoolSize";
		public final static Properties PROPERTIES = System.getProperties();
		public static String getString(String key) {
			return PROPERTIES.getProperty(key);
		}
		public static Integer getInt(String key) {
			try {
            	return Integer.valueOf(PROPERTIES.getProperty(key));
            }catch(Exception e) {
            	return null;
            }
		}
		public static Long getLong(String key) {
			try {
            	return Long.valueOf(PROPERTIES.getProperty(key));
            }catch(Exception e) {
            	return null;
            }
		}
		public static Boolean getBoolean(String key) {
			try {
				String property = PROPERTIES.getProperty(key);
				if(StringUtils.isEmpty(property)) {
					return null;
				}
            	return Boolean.valueOf(PROPERTIES.getProperty(key));
            }catch(Exception e) {
            	return null;
            }
		}
		public static String[] getArray(String key) {
			String value = getString(key);
			if(StringUtil.isEmpty(value)) {
				return null;
			}
			return value.split(",");
		}
		public static Boolean getDebug() {
			Boolean debug = getBoolean(DEBUG);
			if(null==debug)debug=true;
			return debug;
		}
		private static String getDefaultDbUrl() {
			return DBType.valueOf(DBType.mysql.toString()).createUrl(DEFAULTDBIP, DEFAULTDBPORT, DEFAULTDBSCHEMANAME, null);
		}
		public static String getDbUsername() {
			String username = getString(DBUSERNAME);
			if(StringUtils.isEmpty(username)) {
				username = "root";
			}
			return username;
		}
		public static String getDbPasswd() {
			String passwd = getString(DBPASSWORD);
			if(StringUtils.isEmpty(passwd)) {
				passwd = "123456";
			}
			return passwd;
		}
		public static String getDbDriverClassName() {
			String dbtype = getString(DBTYPE);
			if(StringUtil.isEmpty(dbtype)) {
				dbtype = DBType.mysql.toString();
			}
			return DBType.valueOf(dbtype).createDriverClassName();
		}
		public static String getDbUrl() {
			String dbtype = getString(DBTYPE);
			if(StringUtil.isEmpty(dbtype)) {
				dbtype = DBType.mysql.toString();
			}
			String ip = getString(DBIP);
			Integer port = getInt(DBPORT);
			String schema = getString(DBSCHEMA);
			if(StringUtil.isEmpty(schema)) {
				schema = DEFAULTDBSCHEMANAME;
			}
			String url = DBType.valueOf(dbtype).createUrl(ip, port, schema, null);
		    if(StringUtils.isEmpty(url)) {
		    	url = getDefaultDbUrl();
		    }
		    return url;
		}
		public static String getWebPath() {
			return getString(WEBPATH);
		}
		public static Integer getWebPort() {
			Integer port = getInt(WEBPORT);
			if(null==port)port=8888;
			return port;
		}
		public static Integer accesslogPoolSize() {
			Integer poolsize = getInt(ACCESSLOGPOOLSIZE);
			if(null==poolsize)poolsize=10;
			return poolsize;
		}
	}
}
